<?php

App::uses('TestEnvironment', 'Lib');
App::uses('FileSystem', 'Base.Lib');
App::uses('ConfigurationKeys', 'ConfigurationKeys.Lib');
App::uses('PsfmAdmin', 'Lib');

class TagTest extends CakeTestCase {

    public $fixtures = array(
        'plugin.ConfigurationKeys.SettedConfigurationKey',
        'RawFile',
        'RawFileTag',
        'SourceDocument',
        'SourceDocumentTag',
        'Tag',
    );

    /**
     *
     * @var RawFile
     */
    private $RawFile;

    /**
     *
     * @var Tag
     */
    private $Tag;

    /**
     *
     * @var RawFileTag
     */
    private $RawFileTag;

    public function setUp() {
        parent::setUp();

        ConfigurationKeys::setKeyValue(
            'raw_files_directory'
            , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'shared_files_directory'
                , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'joined_document_regex'
                , '/^([\S]+)/'
        );

        PsfmAdmin::customDataModelsClearCache();
        $this->RawFile = ClassRegistry::init('RawFile');
        $this->RawFileTag = ClassRegistry::init('RawFileTag');
        $this->Tag = ClassRegistry::init('Tag');
    }

    public function tearDown() {
        FileSystem::recursiveRemoveDirectory(
                ConfigurationKeys::getKeyValue('raw_files_directory')
        );

        FileSystem::recursiveRemoveDirectory(
                ConfigurationKeys::getKeyValue('shared_files_directory')
        );

        parent::tearDown();
    }

    public function testCreate() {
        $tagName = 'raw_file_test';
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('shared_files_directory') . DS . $tagName), false);
        $this->RawFile->create();
        $result = $this->Tag->save(array(
            $this->Tag->alias => array(
                'name' => $tagName
            )
        ));

        $this->assertEqual($this->Tag->validationErrors, array());
        $this->assertNotEqual($result, false);

        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('shared_files_directory') . DS . $tagName), true);
        $tag = $this->Tag->findByName($tagName);
        $this->assertEqual(empty($tag), false);
    }

    public function testRename() {
        //Check no rows saved
        $this->assertEqual($this->RawFile->find('count'), 0);
        $this->assertEqual($this->RawFileTag->find('count'), 0);
        $this->assertEqual($this->Tag->find('count'), 0);

        //Create raw file
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . 'document file 1'), false);
        $this->RawFile->create();
        $result = $this->RawFile->save(array(
            $this->RawFile->alias => array(
                'name' => 'document file 1'
            )
        ));
        $this->assertEqual($this->RawFile->validationErrors, array());
        $this->assertNotEqual($result, false);
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . 'document file 1'), true);
        $rawFile = $this->RawFile->find('first');

        //Create raw file tag
        $this->Tag->create();
        $this->Tag->save(array(
            'Tag' => array(
                'name' => 'Tag test'
            )
        ));
        $tag = $this->Tag->read();
        $this->assertEqual(empty($tag), false);

        $rawFileTag['RawFileTag'] = array(
            'raw_file_id' => $rawFile['RawFile']['id'],
            'tag_id' => $tag['Tag']['id'],
            'name' => $rawFile['RawFile']['name']
        );
        $this->RawFileTag->create();
        $this->RawFileTag->save($rawFileTag);
        $rawFileTag = $this->RawFileTag->read();
        $this->assertEqual(empty($rawFileTag), false);

        $this->assertEqual($this->RawFile->find('count'), 1);
        $this->assertEqual($this->RawFileTag->find('count'), 1);
        $this->assertEqual($this->Tag->find('count'), 1);
        
        $rawFileTagLinkPrevious = $this->RawFileTag->absolutePath($rawFileTag);
        $this->assertEqual(file_exists($rawFileTagLinkPrevious), true);
        $this->assertEqual(is_link($rawFileTagLinkPrevious), true);
        
        $tagDirectoryPrevious = $this->Tag->absolutePath($tag);
        $this->assertEqual(is_dir($tagDirectoryPrevious), true);

        //Rename tag fle
        $this->assertEqual($tag['Tag']['name'], 'Tag test');
        $tag['Tag']['name'] = 'Tag name changed';
        $result = $this->Tag->save($tag);
        $this->assertEqual($this->Tag->validationErrors, array());
        $this->assertNotEqual($result, false);

        $this->assertEqual($this->RawFile->find('count'), 1);
        $this->assertEqual($this->RawFileTag->find('count'), 1);
        $this->assertEqual($this->Tag->find('count'), 1);

        $rawFile = $this->RawFile->find('first');
        $this->assertEqual($rawFile['RawFile']['name'], 'document file 1');
        $rawFileTag = $this->RawFileTag->find('first');
        $this->assertEqual($rawFileTag['RawFileTag']['name'], 'document file 1');
        $tag = $this->Tag->find('first');
        $this->assertEqual($tag['Tag']['name'], 'Tag name changed');
        
        $this->assertEqual(file_exists($rawFileTagLinkPrevious), false);
        $this->assertEqual(file_exists($tagDirectoryPrevious), false);
    }

}